import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_video_course.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:get/get.dart';

import '../../../bean/bean_train.dart';
import '../controller/course_sign_recoder_controller.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/13.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///课程简介
class CourseSignAllListPage extends StatefulWidget {
  final BeanVideoCourse beanVideoCourse;
  final int trainId;

  const CourseSignAllListPage({
    Key key,
    this.beanVideoCourse,
    this.trainId,
  }) : super(key: key);

  @override
  _CourseSignAllListPageState createState() => _CourseSignAllListPageState();
}

class _CourseSignAllListPageState extends State<CourseSignAllListPage> {
  CourseSignRecoderController _signRecoderController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _signRecoderController = Get.put(
        CourseSignRecoderController(widget.beanVideoCourse, widget.trainId),
        tag: "${widget.beanVideoCourse.id}");
    Future.delayed(Duration.zero, () {
      _signRecoderController.initRequest();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("签到记录"),
      ),
      body: GetBuilder(
        id: "root",
        tag: "${widget.beanVideoCourse.id}",
        builder: (CourseSignRecoderController controller) {
          bool isLoading = controller.isLoading;
          if (isLoading) {
            return LoadingWidget();
          }
          List<SignRecoderBean> recoderList = controller.recoderList;
          if (recoderList.isEmpty) {
            return NoDataWidget();
          }

          return ListView.builder(
            padding: EdgeInsets.only(top: 10),
            itemCount: recoderList.length,
            itemBuilder: (BuildContext context, int index) {
              SignRecoderBean itemBean = recoderList[index];
              return Container(
                decoration: BoxDecoration(
                  color: Colors.grey[200],
                  borderRadius: BorderRadius.all(Radius.circular(4)),
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Row(
                      children: [
                        Expanded(
                          child:
                              Text("${itemBean.signToTime.substring(0, 10)}"),
                        ),
                        buildStatuesWidget(itemBean),
                        SizedBox(width: 10),
                      ],
                    ),
                    SizedBox(height: 10),
                    Row(
                      children: [
                        Image.asset(
                          "assets/images/3.0x/sign_sta_icon.png",
                          width: 14,
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        Text(
                          "签到 ${itemBean.signToTime}",
                          style:
                              TextStyle(fontSize: 12, color: Colors.grey[600]),
                        )
                      ],
                    ),
                    SizedBox(height: 8),
                    Row(
                      children: [
                        Image.asset(
                          "assets/images/3.0x/sign_sta_icon.png",
                          width: 14,
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        Text(
                          "签退 ${itemBean.signUpTime ?? "--"}",
                          style:
                              TextStyle(fontSize: 12, color: Colors.grey[600]),
                        )
                      ],
                    ),
                  ],
                  crossAxisAlignment: CrossAxisAlignment.start,
                ),
                margin: EdgeInsets.only(left: 12, right: 5, bottom: 8),
                padding: EdgeInsets.all(12),
                alignment: Alignment.centerLeft,
              );
            },
          );
        },
      ),
    );
  }

  buildStatuesWidget(SignRecoderBean itemBean) {
    int statues = itemBean.statues ?? 0;
    if (statues == 0) {
      return Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            Icons.access_alarm,
            size: 12,
          ),
          SizedBox(
            width: 8,
          ),
          Text(
            "未签到",
            style: TextStyle(fontSize: 12, color: Colors.grey),
          ),
        ],
      );
    } else if (statues == 1) {
      return Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            Icons.access_alarm,
            size: 12,
            color: Colors.green,
          ),
          SizedBox(
            width: 8,
          ),
          Text(
            "已签到",
            style: TextStyle(fontSize: 12, color: Colors.green),
          ),
        ],
      );
    }
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Icon(
          Icons.access_alarm,
          size: 12,
          color: Colors.blue,
        ),
        SizedBox(
          width: 8,
        ),
        Text(
          "已签退",
          style: TextStyle(fontSize: 12, color: Colors.blue),
        ),
      ],
    );
  }
}
